﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucKinhNghiem : UserControl
    {
        private String IDNV=null ;
        private String ID_Co;
        public ucKinhNghiem()
        {
            InitializeComponent();
            ShowData();
            
        }
        public ucKinhNghiem(string IDNV)
        {
            InitializeComponent();
            this.IDNV = IDNV;
            ShowData();
            ShowDataGrid();
           
        }
        void ShowData()
        {
            PositionBUO ps = new PositionBUO();
            cbViTri.DataSource = ps.GetAll();
            cbViTri.ValueMember = "PositionId";
            cbViTri.DisplayMember = "PositionName";

            NhanVienBUO nv = new NhanVienBUO ();
            lblNhanVien.Text = nv.GetSingle(int.Parse(IDNV)).FirstName + " " + nv.GetSingle(int.Parse(IDNV)).LastName;
        }
        void ShowDataGrid()
        {
            KinhNghiemBUO kn = new KinhNghiemBUO();
            dataGridView1.DataSource = kn.GetALlByID(int.Parse(IDNV)).Select(p=> new {ID=p.ExperenceID, NgayBatDau =p.startDate,NgayKetThuoc=p.finishDate, NoiLamViec =p.Orgization}).ToList();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tbl_Experence kn = new tbl_Experence();
            KinhNghiemBUO knbkq = new KinhNghiemBUO();
            kn.Position = int.Parse(cbViTri.SelectedValue.ToString());
            kn.Id_Staff = int.Parse(IDNV);
            kn.startDate = dtpBatDau.Text;
            kn.finishDate = dtpKetThuc.Text;
            kn.Orgization = txtNoi.Text;
            if (knbkq.Insert(kn))
            {
                MessageBox.Show("Thành công");

            }
            else
            {
                MessageBox.Show("Thất bại ");
            }
            ShowDataGrid();
           

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null;




            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            KinhNghiemBUO kn = new KinhNghiemBUO();

            DialogResult ds = MessageBox.Show("Co nen xoa hay khong", "Xoa du lieu", MessageBoxButtons.YesNo);
            if (ds == DialogResult.Yes)
            {
                if (kn.Delete(int.Parse(ID_Co)))
                {
                    MessageBox.Show("Xoa thanh cong");
                }
                else
                {
                    MessageBox.Show("Xoa that bai");
                }
            }
            else
            {

            }
            ShowDataGrid();

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ControlAcess.ShowConTrol(this.ParentForm, new ucQuanLyNhanVien(), "gbHienThi");
        }

       

        /*
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            nhv=new NhanVienBUO();
            vt = new PositionBUO();
            if (nhv == null)
                return;
            dataGridView1.Columns[6].Visible = false;
            dataGridView1.Columns[7].Visible = false;
            foreach (DataGridViewRow row  in dataGridView1.Rows)
            {
                if (row.Index != 0)
                {
                    try
                    {
                        Type a = typeof(String);
                        int i = int.Parse(row.Cells[2].Value.ToString());
                        String newValue = vt.GetSingle(i).PositionName.ToString();
                       // row.Cells[2] = a;
                        row.Cells[2].Value = 9;
                    }
                    catch (Exception)
                    {
                        
                        throw;
                    }
                   
                }
            }
            

        }

         * */
    }
}
